


const {
    dict_controller_save,
    dict_controller_find,
    dict_controller_get,
    dict_controller_delete,
    dict_controller_getDictConfig
} = require("../../../controller/system/dict/DICT.CONTROLLER");
const { emptyCheck } = require("../../../utils/checkEmpty");
const pageSizeList = require("../../../utils/slicePageSize");
const { serverError } = require("../../../utils");
/**
 * @description 字典路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/system/dict");

// 添加或编辑
router.post("/create", async (ctx, next) => {
    try {
        console.log("发过来的参数body", ctx.request.body)
        const {        // shopId,
            dictNo,
            dictName = undefined,
            dictType = undefined,
            remark = undefined,
            status = undefined,
        } = ctx.request.body
        const check = emptyCheck({ dictName, dictType, })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await dict_controller_save({
                dictNo,
                dictName,
                dictType,
                remark,
                status,
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})
/**@description 根据id获取字典详情 */
router.get("/get", async (ctx, next) => {
    try {
        const dictNo = ctx.query.dictNo || undefined;
        const res = await dict_controller_get({ dictNo });
        console.log("查询：\n", dictNo)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据dictNo删除字典 */
router.delete("/delete", async (ctx, next) => {
    try {
        const dictNo = ctx.query.dictNo || undefined;
        const res = await dict_controller_delete({ ids: [dictNo] });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 列表查询 */
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, dictName, dictType = null } = ctx.request.body;
        const res = await dict_controller_find({
            current, pageSize, dictName, dictType
        })
        ctx.body = res;
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})

/**@description 获取字典配置文件JSON */
router.get("/getDictConfig", async (ctx, next) => {
    try {
        const res = await dict_controller_getDictConfig();
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }

})
module.exports = router